﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArtClub.DBAL;
using ArtClub.Controller;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Text;

namespace ArtClub
{
    public partial class UpdateCatalogue : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                PieceCtr control = new PieceCtr();
                Piece prod = control.getPiece(Convert.ToInt32(Request.QueryString["id"]));
                if (prod != null)
                {
																				LabelArtName.Text = prod.name;
																				LabelArtistName.Text = prod.artist;
																				LabelArtType.Text = prod.type;
																				LabelDescription.Text = prod.description;
																				LabelOriginalPrice.Text = prod.originalPrice.ToString();
																				LabelCreatedBy.Text = prod.createdBy;
																				LabelDate.Text = prod.date.ToString();
																				imgProduct.ImageUrl = prod.image;
                }
            }
												else
												{
														Response.Redirect("BrowseCatalogue.aspx");
												}

        }

        protected void updateLabels(int id)
        {
										PieceCtr ctr = new PieceCtr();
										Piece p = ctr.getPiece(id);
										if (p != null)
										{
												LabelArtName.Text = p.name;
												LabelArtistName.Text = p.artist;
												LabelArtType.Text = p.type;
												LabelOriginalPrice.Text = p.originalPrice.ToString();
												LabelCreatedBy.Text = p.createdBy;
												LabelDate.Text = p.date.ToString();
												LabelDescription.Text = p.description;
										}
        }

        protected void buttonSubmit_clicked(object sender, EventArgs e)
        {
										try
										{
												HttpPostedFile file = Request.Files["myFile"];

												//To separate the location properties.
												string fname = "";
												string path = "";
												string extension = "";

												//Checks if a valid file was submitted.
												if (file != null && file.ContentLength > 0)
												{
														fname = Path.GetFileName(file.FileName);
														extension = fname.Split('.').Last();

														//To check if the file has an appropriate extension.
														string[] whiteList = new string[] { "png", "gif", "jpg", "bmp" };

														if (fname == string.Empty || extension == string.Empty || !whiteList.Contains(extension.ToLower()))
														{
																throw new Exception("Invalid file type, should be an image.");
														}

														//fname = CalculateMD5Hash(fname) + "." + extension;
														path = Path.Combine("../Uploads/", fname);
														file.SaveAs(Server.MapPath(path));
												}

												PieceCtr ctr = new PieceCtr();
												ctr.updatePiece(Convert.ToInt32(Request.QueryString["id"]), ArtNameInput.Text, ArtistNameInput.Text, DescriptionInput.Text,
														TypeOfArtInput.Text, Convert.ToDouble(OriginalPriceValueInput.Text), path,
														CreatedByInput.Text, DateTime.Now);

												cleanTextField();
												Response.Redirect("UpdateCatalogue.aspx?id=" + Request.QueryString["id"]);
										}
										catch (Exception exep)
										{
												Page.ClientScript.RegisterClientScriptBlock(GetType(), "asdf", "bootbox.alert('Update piece failed: " + exep.Message + "')", true);
												cleanTextField();
										}

        }

        private void cleanTextField()
        {
            ArtNameInput.Text = string.Empty;
            ArtistNameInput.Text = string.Empty;
            DescriptionInput.Text = string.Empty;
            TypeOfArtInput.Text = string.Empty;
            OriginalPriceValueInput.Text = string.Empty;
            CreatedByInput.Text = string.Empty;
            DateInput.Text = string.Empty;
        }

    }
}